import Enity from "./Enity.js"
import { bulletImg } from "../image.js";
//子弹类
class Bullet extends Enity {
    type = 'bullet'
    name = "子弹"
    img = bulletImg;//所使用的图片
    v = 10;//速度
    atkTarget = []//子弹可以攻击的对象,传入type值，当子弹与传入的类型对象发生碰撞时，会扣除目标的hp
    atk = 0.5//子弹的攻击力
    orientation = 0;//自动攻击的角度弧度制
    constructor(x, y, orientation = 0,arkTarget = ['enemy']) {
        super(x, y, 10)
        this.orientation = orientation
        this.atkTarget = arkTarget
    }
    //显示图片
    draw = function (ctx) {
        ctx.save()
        ctx.rotate(this.orientation)
        //由于坐标轴进行了旋转，所以这里要计算出新的坐标轴上与原来坐标轴所对应的坐标位置。
        let r = this.nodeDistance({ x: 0, y: 0 })
        let x = r * Math.sin(Math.PI / 2 - Math.acos(this.x / r) + this.orientation)
        let y = r * Math.cos(Math.PI / 2 - Math.acos(this.x / r) + this.orientation)
        // ctx.drawImage(this.img, this.x - this.R, this.y - this.R, this.R * 2, this.R * 2)
        ctx.drawImage(this.img, x - this.R, y - this.R, this.R * 2, this.R * 2)
        ctx.restore()
    }
    //行为
    behavior = function (context) {
        this.move(context)//移动
        context = this.hit(context)
        return context
    }
    //移动方式
    move = function () {
        // this.y -= this.v
        this.y += -this.v * Math.cos(this.orientation)
        this.x += this.v * Math.sin(this.orientation)
    }
    //撞击判断
    hit = function (context) {
        context.NodeList.forEach(target => {
            if (this.atkTarget.includes(target.type)) {//判断是否符合期望目标
                //判断是否已经和目标发送碰撞
                if (this.nodeDistance(target) < this.R + target.R) {
                    //判断目标是否有hp属性，如果有则减去目标的hp
                    if (target.hp) {
                        target.hp -= this.atk
                        //子弹销毁
                        this.OK = false
                    }
                }
            }
        })
        return context
    }
    //状态判断
    isOK(context) {
        if (this.y < -10) {
            this.OK = false
        }
        if (this.y > 600) {
            this.OK = false
        }
        if (this.x < -10) {
            this.OK = false
        }
        if (this.x > 600) {
            this.OK = false
        }
        return context
    }
}
export default Bullet